iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 15
0
Modern Web

把前後分離製作的網站組起來系列 第 15

然後~準備到REST APIs

  • 分享至 

  • xImage
  •  

這篇是要延續第13天
把Spring Boot從JPA延續到REST APIs
https://ithelp.ithome.com.tw/upload/images/20201027/20119035Eua2R79jLH.png

要建立一些packagec和一些java檔..
然後一些java檔是做interface

變數的意思(取名字是很重要的)
id 排第幾個
sku 通常表示規格、顏色、款式
name ,名字
description描述
unit_price 價格
image_url 照片放的位置
active 這邊基本上都打1還在想如何解釋
units_in_stock 庫存數
date_created 被創建的日期
last_updated 最後更新時間


import lombok.Data;
import org.springframework.data.annotation.Id;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;

@Entity
@Table(name="product")
@Data
public class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    private String sku;

    private String name;

    private String description;

    private BigDecimal unitPrice;

    private String imageUrl;

    private boolean active;

    private int unitsInStock;

    private Date dateCreated;

    private Date lastUpdated;


}


其中@Table(name="product")的product如果反紅代表沒有連到資料庫
然後其他地方反紅就是要按"alt+enter"讓他import
https://ithelp.ithome.com.tw/upload/images/20201027/20119035nHpsCznFVI.png

在每個變數上都加上 @Column(name = "變數")這個變數是對應資料庫的命名
https://ithelp.ithome.com.tw/upload/images/20201027/20119035daRU3y0A9S.png


import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;

@Entity
@Table(name="product")
@Data

public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "sku")
    private String sku;

    @Column(name = "name")
    private String name;

    @Column(name = "description")
    private String description;

    @Column(name = "unit_price")
    private BigDecimal unitPrice;

    @Column(name = "image_url")
    private String imageUrl;

    @Column(name = "active")
    private boolean active;

    @Column(name = "units_in_stock")
    private int unitsInStock;

    @Column(name = "date_created")
    @CreationTimestamp
    private Date dateCreated;

    @Column(name = "last_updated")
    @UpdateTimestamp
    private Date lastUpdated;

}

然後跟日期date有關的地方還要特別加上

    @CreationTimestamp
    private Date dateCreated;

    @Column(name = "last_updated")
    @UpdateTimestamp
    private Date lastUpdated;


在entity package的下面再建一個ProductCategory
@Entity
@Table(name="productcategory")
都KEY上但是到data的地方就不同了
https://ithelp.ithome.com.tw/upload/images/20201027/20119035aisGizdw9t.png

https://ithelp.ithome.com.tw/upload/images/20201030/20119035onA8tdxDJF.png



import lombok.Getter;
import lombok.Setter;

import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Set;

@Entity
@Table(name="product_category")

@Getter
@Setter
public class ProductCategory {

    private  Long id;

    
    private String categoryName;

    private Set<Product> prodict;

}

/images/emoticon/emoticon03.gif

把它完成好準備建立dao進入REST APIs



import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.Set;

@Entity
@Table(name="product_category")

@Getter
@Setter
public class ProductCategory {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    
    private  Long id;

    @Column(name = "category_name")
    private String categoryName;

    @OneToMany(cascade = CascadeType.ALL,mappedBy = "category")
    private Set<Product> products;

}

@OneToMany的意思:一對多
這裡的 @OneToMany(cascade = CascadeType.ALL,mappedBy = "category")會反紅
所以~

再回到Product.java檔新增

    @JoinColumn(name = "category_id",nullable = false)

    private ProductCategory category;

完成JPA~


import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.Date;

@Entity
@Table(name="product")
@Data

public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @ManyToOne
    @JoinColumn(name = "category_id",nullable = false)

    private ProductCategory category;
    @Column(name = "sku")
    private String sku;

    @Column(name = "name")
    private String name;

    @Column(name = "description")
    private String description;

    @Column(name = "unit_price")
    private BigDecimal unitPrice;

    @Column(name = "image_url")
    private String imageUrl;

    @Column(name = "active")
    private boolean active;

    @Column(name = "units_in_stock")
    private int unitsInStock;

    @Column(name = "date_created")
    @CreationTimestamp
    private Date dateCreated;

    @Column(name = "last_updated")
    @UpdateTimestamp
    private Date lastUpdated;

}


https://ithelp.ithome.com.tw/upload/images/20201104/20119035jolob99Vp2.png

因為晚上會晚點回家~
所以我先發表~

DEAR ALL 我們明天見~/images/emoticon/emoticon07.gif


上一篇
這篇是要建變數~
下一篇
創造~REST APIs
系列文
把前後分離製作的網站組起來30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言